#!/usr/bin/env bash
#############################################
tmoe_main() {
	case "$1" in
	p* | proot)
		source_tmoe_env
		TMOE_CHROOT='false'
		;;
	c* | chroot)
		source_tmoe_env
		TMOE_CHROOT='true'
		;;
	systemd | sd | systemctl | ns | nspawn)
		source_tmoe_env
		SYSTEMD_NSPAWN='true'
		TMOE_CHROOT='true'
		;;
	zsh)
		zsh-i
		exit 0
		;;
	theme)
		zshtheme
		exit 0
		;;
	aria2)
		aria2-i
		exit 0
		;;
	i | -i)
		debian-i
		exit 0
		;;
	m | manager | "")
		check_manager_dir
		bash ${MANAGER_DIR}/manager.sh
		exit 0
		;;
	t | tool)
		check_manager_dir
		if [[ $(uname -o) = Android ]]; then
			bash ${MANAGER_DIR}/manager.sh
		else
			bash ${MANAGER_DIR}/tool.sh
		fi
		exit 0
		;;
	-h* | --h* | *)
		cat <<-EOF
			m          --tmoe manager
			t          --tmoe tool
			p          --proot
			c          --chroot
			aria2      --aria2 manager
			theme      --zshtheme
			zsh        --zsh manager
		EOF
		printf "%s\n" "请前往git仓库获取帮助信息"
		exit 0
		;;
	esac
	case "$2" in
	a | arch) DISTRO_NAME='arch' ;;
	al | ap | alpine) DISTRO_NAME='alpine' ;;
	arm | armbian) DISTRO_NAME='armbian' ;;
	c | ce | cent | centos) DISTRO_NAME='centos' ;;
	d | deb | debian) DISTRO_NAME='debian' ;;
	dde | deepin) DISTRO_NAME='deepin' ;;
	devuan) DISTRO_NAME='devuan' ;;
	f | fe | fedora) DISTRO_NAME='fedora' ;;
	ft | funtoo) DISTRO_NAME='funtoo' ;;
	g | gt | gentoo) DISTRO_NAME='gentoo' ;;
	k | kali) DISTRO_NAME='kali' ;;
	mi | mint) DISTRO_NAME='mint' ;;
	m | mj | mjr | manjaro) DISTRO_NAME='manjaro' ;;
	o | suse | opensuse) DISTRO_NAME='opensuse' ;;
	ow | wrt | openwrt) DISTRO_NAME='openwrt' ;;
	r | rasp | raspios) DISTRO_NAME='raspios' ;;
	raspbian) DISTRO_NAME='raspios' ;;
	s | sl | slackware) DISTRO_NAME='slackware' ;;
	u | ub* | ubuntu) DISTRO_NAME='ubuntu' ;;
	v | void) DISTRO_NAME='void' ;;
	vnc) EXEC_PROGRAM='startvnc' ;;
	xs | xsdl | xserver) EXEC_PROGRAM='startxsdl' ;;
	x11 | x11vnc) EXEC_PROGRAM='startx11vnc' ;;
	no | novnc) EXEC_PROGRAM='novnc' ;;
	*) DISTRO_NAME="$2" ;;
	esac
	case "$3" in
	20.10) DISTRO_CODE='groovy' ;;
	21.04) DISTRO_CODE='hirsute' ;;
	20.04) DISTRO_CODE='focal' ;;
	18.04) DISTRO_CODE='bionic' ;;
	8s) DISTRO_CODE='8-Stream' ;;
	9s) DISTRO_CODE='9-Stream' ;;
	ro) DISTRO_CODE='rolling' ;;
	s) DISTRO_CODE='sid' ;;
	r) DISTRO_CODE='rolling' ;;
	raw) DISTRO_CODE='rawhide' ;;
	n | none) DISTRO_CODE='' ;;
	x | amd64 | x64) ARCH_TYPE='amd64' ;;
	a | aarch64 | arm64) ARCH_TYPE='arm64' ;;
	h | arm | armhf) ARCH_TYPE='armhf' ;;
	armel) ARCH_TYPE='armel' ;;
	i | i386 | x86 | x32) ARCH_TYPE='i386' ;;
	p | ppc) ARCH_TYPE='ppc64el' ;;
	s390x) ARCH_TYPE='s390x' ;;
	m | mips | mipsel) ARCH_TYPE='mipsel' ;;
	m64 | mips64 | mips64el) ARCH_TYPE='mips64el' ;;
	risc*) ARCH_TYPE='riscv64' ;;
	v | vnc) EXEC_PROGRAM='tigervnc' ;;
	xs | xsdl | xserver) EXEC_PROGRAM='xserver' ;;
	x11 | x11vnc) EXEC_PROGRAM='x11vnc' ;;
	no | novnc) EXEC_PROGRAM='novnc' ;;
	bash) TMOE_SHELL='/bin/bash' ;;
	zsh) TMOE_SHELL='/bin/zsh' ;;
	ash) TMOE_SHELL='/bin/ash' ;;
	*) DISTRO_CODE="$3" ;;
	esac

	case "$4" in
	x | amd64 | x64) ARCH_TYPE='amd64' ;;
	a | aarch* | arm64) ARCH_TYPE='arm64' ;;
	h | arm | armhf) ARCH_TYPE='armhf' ;;
	armel) ARCH_TYPE='armel' ;;
	i | i386 | x86 | x32) ARCH_TYPE='i386' ;;
	p | ppc*) ARCH_TYPE='ppc64el' ;;
	s390*) ARCH_TYPE='s390x' ;;
	m | mips | mipsel) ARCH_TYPE='mipsel' ;;
	m64 | mips64 | mips64el) ARCH_TYPE='mips64el' ;;
	risc*) ARCH_TYPE='riscv64' ;;
	v*) EXEC_PROGRAM='startvnc' ;;
	xs | xsdl | xserver) EXEC_PROGRAM='startxsdl' ;;
	x11 | x11vnc) EXEC_PROGRAM='startx11vnc' ;;
	no | novnc) EXEC_PROGRAM='novnc' ;;
	bash) TMOE_SHELL='/bin/bash' ;;
	zsh) TMOE_SHELL='/bin/zsh' ;;
	ash) TMOE_SHELL='/bin/ash' ;;
	esac
	case "$5" in
	v*) EXEC_PROGRAM='startvnc' ;;
	x | xs | xsdl | xserver) EXEC_PROGRAM='startxsdl' ;;
	x11 | x11vnc) EXEC_PROGRAM='startx11vnc' ;;
	no | novnc) EXEC_PROGRAM='novnc' ;;
	bash) TMOE_SHELL='/bin/bash' ;;
	zsh) TMOE_SHELL='/bin/zsh' ;;
	ash) TMOE_SHELL='/bin/ash' ;;
	esac
	TMOE_PARAMETERS="$*"
	start_tmoe_container
}
#############
check_manager_dir() {
	MANAGER_DIR_01="${HOME}/.local/share/tmoe-linux/git"
	MANAGER_DIR_02="/usr/local/etc/tmoe-linux/git"
	if [ -e "${MANAGER_DIR_01}" ]; then
		MANAGER_DIR=${MANAGER_DIR_01}
	else
		MANAGER_DIR=${MANAGER_DIR_02}
	fi
}
source_tmoe_env() {
	TMOE_CONTAINER_FORTUNE=true
	TMOE_CONTAINER_HITOKOTO=true
	CONFIG_FOLDER=${HOME}/.config/tmoe-linux
	source ${CONFIG_FOLDER}/hitokoto.conf 2>/dev/null
	case $(uname -o) in
	Android)
		TMOE_LINUX_DIR="${HOME}/.local/share/tmoe-linux"
		[[ ! $(command -v termux-wake-lock) ]] || termux-wake-lock 2>/dev/null
		;;
	*) TMOE_LINUX_DIR="/usr/local/etc/tmoe-linux" ;;
	esac
	case ${TMOE_CONTAINER_FORTUNE} in
	true)
		if [ $(command -v fortune) ]; then
			fortune 2>/dev/null
		elif [ -e /usr/games/fortune ]; then
			/usr/games/fortune 2>/dev/null
		fi
		;;
	esac
	TMOE_GIT_DIR="${TMOE_LINUX_DIR}/git"
	TMOE_SHARE_DIR="${TMOE_GIT_DIR}/share"
	TMOE_CONTAINER_DIR="${TMOE_LINUX_DIR}/containers"
	ENV_FILE="${TMOE_SHARE_DIR}/environment/manager_environment"
	source ${ENV_FILE}
	check_arch
	ACROSS_ARCH_FILE="${CONFIG_FOLDER}/across_architecture_container.txt"
	[[ ! -s "${ACROSS_ARCH_FILE}" ]] || ARCH_TYPE="$(head -n 1 ${ACROSS_ARCH_FILE})"
}
########
start_tmoe_container() {
	DEFAULT_SHELL_CONF=${CONFIG_FOLDER}/default_shell.conf
	case ${TMOE_SHELL} in
	"") rm -f ${DEFAULT_SHELL_CONF} 2>/dev/null ;;
	*) printf "%s\n" "TMOE_SHELL=${TMOE_SHELL}" >${DEFAULT_SHELL_CONF} ;;
	esac
	if [[ $(command -v pulseaudio) && $(id -u) != 0 ]]; then
		pulseaudio --start
	fi
	case "${DISTRO_NAME}" in
	"") ;;
	*)
		case ${DISTRO_CODE} in
		"") DEBIAN_FOLDER=${DISTRO_NAME}_${ARCH_TYPE} ;;
		*) DEBIAN_FOLDER=${DISTRO_NAME}-${DISTRO_CODE}_${ARCH_TYPE} ;;
		esac
		;;
	esac
	tmoe_env_02 #source DEBIAN_CHROOT以及其它变量
	if [ -e ${DEBIAN_CHROOT}/etc/os-release ]; then
		grep 'PRETTY_NAME' ${DEBIAN_CHROOT}/etc/os-release 2>/dev/null | awk -F '"' '{print $2}'
	else
		sed -n p ${DEBIAN_CHROOT}/etc/issue 2>/dev/null
	fi
	case ${TMOE_CONTAINER_HITOKOTO} in
	true) [[ ! ${TMOE_LANG} =~ zh_.*UTF-8 ]] || get_hitokito_cn ;;
	esac
	if [[ -n ${EXEC_PROGRAM} ]]; then
		case ${EXEC_PROGRAM} in
		startx11vnc | startvnc) [[ ! $(command -v am) ]] || am start -n com.realvnc.viewer.android/com.realvnc.viewer.android.app.ConnectionChooserActivity 2>/dev/null ;;
		no | novnc)
			NOVNC_PORT=$(grep '^NOVNC_PORT=' ${DEBIAN_CHROOT}/usr/local/bin/novnc | head -n 1 | awk -F '=' '{print $2}' | cut -d '"' -f 2)
			[[ -n ${NOVNC_PORT} ]] || NOVNC_PORT=36080
			NOVNC_ADDR="http://localhost:${NOVNC_PORT}/vnc.html"
			printf "${YELLOW}%s${RESET}\n" "${NOVNC_ADDR}"
			if [[ $(command -v am) ]]; then
				am start -a android.intent.action.VIEW -d "${NOVNC_ADDR}" 2>/dev/null
			elif [[ $(command -v xdg-open) ]]; then
				xdg-open ${NOVNC_ADDR} 2>/dev/null
			elif [[ -e /mnt/c/WINDOWS/system32/cmd.exe ]]; then
				cd /mnt/c/Users
				/mnt/c/WINDOWS/system32/cmd.exe /c "start ${NOVNC_ADDR}"
				cd -
			fi
			;;
		startxsdl)
			[[ ! $(command -v am) ]] || am start -n x.org.server/x.org.server.MainActivity 2>/dev/null
			printf "%s\n" "Please wait 9s."
			#sleep 9
			#此处不要sleep
			;;
		esac
		${TMOE_PREFIX} touch ${TMOE_GNU_LINUX_CONTAINER_ROOT_VNC_DIR}/${EXEC_PROGRAM}
	fi
	case ${SYSTEMD_NSPAWN} in
	true)
		case $(uname -o) in
		Android)
			printf "%s\n" "Sorry,this feature does not support ${PURPLE}Android${RESET} system."
			printf "${YELLOW}%s\n" "如需运行systemd容器，请换用${BLUE}GNU/Linux${RESET}系统。"
			exit 1
			;;
		esac
		case $(id -u) in
		0)
			source ${TMOE_SHARE_DIR}/removal/umount
			source ${TMOE_SHARE_DIR}/container/nspawn/startup
			;;
		*)
			sudo -E tmoe ${TMOE_PARAMETERS}
			exit 1
			;;
		esac
		;;
	*) ${TMOE_PREFIX} bash ${TMOE_GNU_LINUX_CONTAINER_STARTUP_FILE} ;;
	esac
}
##########
tmoe_main "$@"
